﻿namespace sfcms.api
{
    using System;
    using System.Data;

    public class placeholder
    {
        public string content;
        public string controlid;
        public long id;
        public long pageid;

        public static placeholder bypageandcontrolid(long pageid, string controlid)
        {
            DataTable data = new database().ExecuteDataTable("SELECT * FROM SFCMS_PlaceHolder (NOLOCK) WHERE [PageID] = @0 AND [ControlID] = @1", new object[] { "@0", pageid, "@1", controlid });
            if (data.Rows.Count == 0)
            {
                return null;
            }
            return new placeholder { id = Convert.ToInt64(data.Rows[0]["ID"]), pageid = Convert.ToInt64(data.Rows[0]["PageID"]), controlid = data.Rows[0]["ControlID"].ToString(), content = data.Rows[0]["Content"].ToString() };
        }

        public static placeholder[] getpageplaceholders()
        {
            if (page.current == null)
            {
                return null;
            }
            DataTable data = new database().ExecuteDataTable("SELECT * FROM SFCMS_PlaceHolder (NOLOCK) WHERE [PageID] = @0", new object[] { "@0", page.current.id });
            if (data.Rows.Count == 0)
            {
                return new placeholder[0];
            }
            placeholder[] placeholders = new placeholder[data.Rows.Count];
            for (int i = 0; i < placeholders.Length; i++)
            {
                placeholders[i] = new placeholder();
                placeholders[i].id = Convert.ToInt64(data.Rows[i]["ID"]);
                placeholders[i].pageid = Convert.ToInt64(data.Rows[0]["PageID"]);
                placeholders[i].controlid = data.Rows[i]["ControlID"].ToString();
                placeholders[i].content = data.Rows[i]["Content"].ToString();
            }
            return placeholders;
        }

        public void save()
        {
            if (this.id == 0L)
            {
                this.id = Convert.ToInt64(new database().ExecuteScalar("INSERT INTO SFCMS_PlaceHolder (PageID, ControlID, Content) VALUES (@0, @1, @2)\r\nSELECT SCOPE_IDENTITY()", new object[] { "@0", this.pageid, "@1", this.controlid, "@2", this.content }));
            }
            else
            {
                new database().ExecuteNonQuery("UPDATE SFCMS_PlaceHolder SET Content = @0 WHERE [ID] = @1", new object[] { "@0", this.content, "@1", this.id });
            }
        }
    }
}

